<?php
/**
 * 后台标签管理
 * @package app\admin\controller
 * User: Administrator
 * Date: 2018/10/26 0013
 * Time: 14:55
 */
namespace app\admin\controller;

use service\DataService;
use service\ToolsService;
use think\Db;
use think\Loader;

class Tag extends BasicAdmin
{

    public $table = 'ml_tag';

    /**
     *  标签列表
     */
    public function index()
    {
        $this->title = '网站配置 > 标签管理';
        if($this->request->isPost()){
            $param = $this->request->param();
            $db = Db::name($this->table);
            foreach (['tag'] as $key) {
                (isset($param[$key]) && $param[$key] !== '') && $db->whereLike($key, "%{$param[$key]}%");
            }
            foreach(['from','recommend'] as $key){
                (isset($param[$key]) && $param[$key] !== '') && $db->where($key,$param[$key]);
            }
            if(isset($param['catid']) && $param['catid']) {
                $pid = \app\admin\model\Category::where('id', $param['catid'])->value('pid');
                if($pid > 0 ){
                    $db->where('catid', $param['catid']);
                }else{
                    $catids = \app\admin\model\Category::where('pid', $param['catid'])->column('id');
                    $db->where('catid', 'in',$catids);
                }
            }
            $params = [];
            return parent::_list($db,$params);
        }
        //获取所有用户信息
        $users = \app\admin\model\Member::where('status',1)->field('id,nickname')->select();
        //获取分类
        $_cates = Db::name('ml_cate')->order('sort desc,id desc')->select();
        $cates = ToolsService::arr2table($_cates);
        $this->assign('cates', $cates);
        return $this->fetch('',['title'=>$this->title,'users'=>$users]);
    }

    /**
     * 列表数据处理
     * @param array $data
     */
    protected function _index_data_filter(&$data)
    {
        $froms = array_unique(array_column($data,'from'));
        $list = \app\admin\model\Member::where('id','in',$froms)->column('id,nickname');
        //分类
        $catids = array_unique(array_merge(array_column($data, 'category'), array_column($data, 'catid')));
        $cate_list = \app\admin\model\Category::where('id', 'in', $catids)->column('id,cate');
        foreach ($data as &$vo) {
            $vo['from'] = $vo['from'] > 0 ? $list[$vo['from']] :'系统';
            $vo['catid'] = $vo['catid'] ? $cate_list[$vo['catid']] : '';
        }

    }

    /**
     * 添加标签
     */
    public function add()
    {
        return $this->_form($this->table, 'form');
    }

    /**
     * 编辑标签
     */
    public function edit()
    {
        if($this->request->isPost()){
            $rst = DataService::update($this->table);
            $msg = doReturn('更新成功','更新失败',$rst);
            $this->resultArr($msg);
        }
    }

    /**
     * 删除标签
     */
    public function del()
    {
        if($this->request->isPost()){
            $data = array_filter($this->request->post());
            $validate = Loader::validate('Tag');
            if(!$validate->scene('delete')->check($data)){
                $this->result(null,'0',$validate->getError(),'json');
            }else{
                $status = DataService::update($this->table);
                $ret = doReturn('标签删除成功！','标签删除失败, 请稍候再试!',$status,[],'json');
                $this->resultArr($ret);
            }
        }
    }
}
